﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Web.Script.Serialization;

namespace LLKanWebService.BLL
{
    public static class Tojson
    {
        /// <summary> 
        /// Json序列化,用于发送到客户端 
        /// </summary> 
        public static string ToJson(this object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
            
        }

        /// <summary> 
        /// Json反序列化,用于接收客户端Json后生成对应的对象 
        /// </summary> 
        public static T FromJsonTo<T>(this string jsonString)
        {

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

            T jsonObject = (T)ser.ReadObject(ms);

            ms.Close();

            return jsonObject;

        }


    }
}
